package io.filemanager;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo01FileManager {
    private static List<String> listByKey = new ArrayList<>();
    private static List<String> listByshuffix = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        System.out.println("======文件资源管理=========");
        System.out.println("1.按照关键字搜索");
        System.out.println("2.按照后缀名搜索");
        System.out.println("3.复制");
        System.out.println("4.退出");

        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                searchByKeyWord();
                break;
            case 2:
                searchByNameSuffix();
                break;
            case 3:
                fileCopy();
                break;
            case 4:
                System.exit(0);
                break;
        }
    }

    private static void searchByKeyWord() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想搜索的目录:");
        String srcir = scanner.nextLine();
        System.out.println("请输入要查找的文件:");
        String key = scanner.nextLine();
        File file = new File(srcir);
        listFile1(file, key);
        System.out.println(listByKey);
    }

    public static void listFile1(File file, String key) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                if (file1.getName().contains(key)) {
                    listByKey.add(file1.getName());
                }
                listFile1(file1, key);
            } else {
                if (file1.getName().contains(key)) {
                    listByKey.add(file1.getName());
                }
            }
        }
    }

    private static void searchByNameSuffix() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想查找的目录:");
        String srcdir = scanner.nextLine();
        File file = new File(srcdir);
        System.out.println("请输入想查找文件的后缀名：");
        String suffix = scanner.nextLine();
        listFile02(file, suffix);
        System.out.println(listByshuffix);
    }

    public static void listFile02(File file, String suffix) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                if (file1.getName().endsWith(suffix)) {
                    listByshuffix.add(file1.getAbsolutePath());
                }
                listFile02(file1, suffix);
            } else {
                if (file1.getName().contains(suffix)) {
                    listByshuffix.add(file1.getAbsolutePath());
                }
            }
        }
    }

    private static void fileCopy() throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要复制的文件绝对路径:");
        String srcdir = scanner.nextLine();
        System.out.println("请输入想要复制的目标目录:");
        String tofile = scanner.nextLine();
        FileInputStream fileInputStream = new FileInputStream(srcdir);
        FileOutputStream fileOutputStream = new FileOutputStream(tofile);
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes);
        }
        fileOutputStream.flush();
    }
}
